<template>
  <div class="layout">
    <div class="layout-content">
      <header-box></header-box>
      <div class="width-percent-100 content-box flex-par">
        <el-menu class="contentLeft" :default-active="typeId" @select="changeType">
          <el-menu-item class="active-style" index="-1">
            <i class="el-icon-menu"></i>
            <span>全部</span>
          </el-menu-item>
          <template v-for="item in typeMenuList" :key="item">
            <el-sub-menu v-if="item.children && item.children.length > 0" :index="item.webMenuId">
              <template #title>
                <i class="el-icon-folder-opened"></i>
                {{ item.menuName }}
              </template>
              <el-menu-item class="active-style" v-for="child in item.children" :key="child" :index="child.webMenuId">
                <i class="el-icon-document"></i>
                {{ child.menuName }}
              </el-menu-item>
            </el-sub-menu>
            <el-menu-item v-else class="active-style" :index="item.webMenuId">
              <i class="el-icon-folder"></i>
              <span>{{ item.menuName }}</span>
            </el-menu-item>
          </template>
        </el-menu>
        <div class="yingyong-page">
          <div class="page-title-box margin-b-10 flex-par">
            <img src="../assets/image/icon2.png" class="title-icon" alt="">
            <div>精选应用</div>
          </div>
          <div class="list-content white-bk">
            <div class="sort-box">
              <el-radio-group v-model="searchForm.sortBy" @change="getPageList(1)" size="small">
                <el-radio-button label="id" border>默认</el-radio-button>
                <el-radio-button label="time" border>时间排序</el-radio-button>
                <el-radio-button label="downloadCount" border>下载量排序</el-radio-button>
                <el-radio-button label="heat" border>热度排序</el-radio-button>
              </el-radio-group>
            </div>
            <div class="list-box">
              <!-- 空状态提示 -->
              <div v-if="tableData.length === 0" class="empty-state">
                <div class="empty-icon">⭐</div>
                <div class="empty-title">暂无精选应用</div>
                <div class="empty-desc">管理员还没有设置任何精选应用~</div>
              </div>
              <!-- 应用列表 -->
              <div class="app-card" v-for="(item, index) in tableData" :key="item" v-else :style="{ 'animation-delay': index * 0.1 + 's' }">
                <!-- 卡片头部 -->
                <div class="app-card-header">
                  <div class="app-icon">{{ getAppIcon(item.rpaRobotName) }}</div>
                  <div class="app-title">
                    {{ item.rpaRobotName }}
                    <span class="featured-badge">⭐</span>
                  </div>
                </div>
                
                <!-- 卡片内容 -->
                <div class="app-card-content">
                  <div class="app-description">{{ item.content || '智能RPA机器人，提升工作效率，自动化处理重复性任务。' }}</div>
                  
                  <!-- 信息行 - 2x3布局 -->
                  <div class="app-info-section">
                    <div class="info-row">
                      <div class="info-item" title="开发者">
                        <span class="info-icon">👤</span>
                        <span class="info-value">{{ item.developerName || '系统管理员' }}</span>
                      </div>
                      <div class="info-item" title="上传时间">
                        <span class="info-icon">📅</span>
                        <span class="info-value">{{ item.uploadDate }}</span>
                      </div>
                      <div class="info-item" title="浏览量">
                        <span class="info-icon">👁️</span>
                        <span class="info-value">{{ item.pageView || 0 }}</span>
                      </div>
                    </div>
                    <div class="info-row">
                      <div class="info-item" title="下载量">
                        <span class="info-icon">⬇️</span>
                        <span class="info-value">{{ item.downloadCount || 0 }}</span>
                      </div>
                      <div class="info-item" title="热度 (下载量×2+浏览量)">
                        <span class="info-icon">🔥</span>
                        <span class="info-value">{{ item.heat || 0 }}</span>
                      </div>
                      <div class="info-item" title="业务分类">
                        <span class="info-icon">🏷️</span>
                        <span class="info-value">{{ item.businessTypeName }}</span>
                      </div>
                    </div>
                  </div>
                </div>
                
                <!-- 操作按钮区域 -->
                <div class="app-card-actions">
                  <button class="action-btn secondary-btn" @click="toDetail(item.id)">
                    <span class="btn-icon">👁️</span>
                    <span>查看详情</span>
                  </button>
                  <button class="action-btn primary-btn" @click="downloadRpa(item.rpaFileAddress)">
                    <span class="btn-icon">⬇️</span>
                    <span>直接下载</span>
                  </button>
                </div>
              </div>
            </div>
            <div class="pagination-box flex-par">
              <el-pagination class="flex-par" :currentPage="searchForm.pageNum" :page-size="searchForm.pageSize"
                :small="true" background="#F4F4F4" layout="total,  prev, pager, next" :total="dataTotal"
                @current-change="getPageList" />
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import headerBox from '@/components/headerBox';
import { bizTypeMenu, getFeaturedApps, pageSize, serverUrl } from '@/assets/api/api.js';

export default {
  data: function () {
    return {
      serverUrl,
      typeMenuList: [],
      typeId: '-1',
      tableData: [],
      dataTotal: 0,
      searchForm: {
        bizType: '-1',
        subBizType: '',
        pageNum: 1,
        pageSize: 12,
        sortBy: 'id',
      },
    }
  },
  components: {
    headerBox
  },
  mounted() {
    this.getBizTypes()
    this.getPageList(1)
  },
  methods: {
    downloadRpa(url) {
      window.open(serverUrl + url)
    },
    toDetail(id) {
      this.$router.push({ path: '/yingyongDetail/' + id });
    },
    // 获取精选应用列表
    getPageList(pageNum) {
      if (pageNum) {
        this.searchForm.pageNum = pageNum
      }
      
      const params = {
        pageNum: Number(this.searchForm.pageNum) || 1,
        pageSize: Number(this.searchForm.pageSize) || 10,
        sortBy: this.searchForm.sortBy || 'id',
        bizType: this.typeId == -1 ? '' : this.typeId
      }
      
      getFeaturedApps(params)
        .then((res) => {
          console.log(res)
          if (res.code == 0) {
            this.tableData = res.data
            this.dataTotal = res.total
          } else {
            this.tableData = []
            this.dataTotal = 0
          }
          this.scrollToTop();
        })
        .catch((err) => { 
          this.tableData = []
          this.dataTotal = 0
          this.scrollToTop();
        });
    },
    changeType(v, v2) {
      console.log(v)
      console.log(v2)
      this.typeId = v
      this.getPageList(1)
    },
    getBizTypes() {
      bizTypeMenu()
        .then((res) => {
          console.log(res)
          if (res.code == 0) {
            this.typeMenuList = res.data.children
          }
        })
        .catch((err) => { });
    },
    // 根据应用名称获取对应的图标
    getAppIcon(appName) {
      const iconMap = {
        '监察': '🔍',
        '监控': '📊',
        '日报': '📋',
        '发票': '🧾',
        '配电': '⚡',
        '设备': '🔧',
        '数据': '📈',
        '财务': '💰',
        '人事': '👥',
        '客服': '🎧',
        '销售': '💼',
        '生产': '🏭',
        '质量': '✅',
        '安全': '🛡️',
        '物流': '🚚',
        '库存': '📦',
        '采购': '🛒',
        '合同': '📄',
        '审批': '✍️',
        '统计': '📊'
      };
      
      // 根据应用名称关键词匹配图标
      for (const [keyword, icon] of Object.entries(iconMap)) {
        if (appName.includes(keyword)) {
          return icon;
        }
      }
      
      // 默认图标
      return '🤖';
    },
    // 滚动到页面顶部
    scrollToTop() {
      this.$nextTick(() => {
        const listContent = document.querySelector('.list-content');
        if (listContent) {
          listContent.scrollTop = 0;
        }
        window.scrollTo({
          top: 0,
          behavior: 'smooth'
        });
      });
    },
  },
};
</script>
<style scoped>
/* 复制应用中心页面的所有样式 */
.layout {
  height: 100%;
}

.layout-content {
  height: 100%;
  width: 100%;
  background: white;
}

.content-box {
  height: calc(100% - 70px);
  width: 100%;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  position: relative;
  overflow: hidden;
}

.content-box::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: radial-gradient(circle at 20% 80%, rgba(120, 119, 198, 0.1) 0%, transparent 50%),
              radial-gradient(circle at 80% 20%, rgba(255, 255, 255, 0.1) 0%, transparent 50%);
  pointer-events: none;
}

.contentLeft {
  background: linear-gradient(180deg, rgba(0, 21, 42, 0.95) 0%, rgba(0, 21, 42, 0.9) 100%);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-right: 1px solid rgba(255, 255, 255, 0.1);
  position: relative;
  z-index: 10;
  box-shadow: 2px 0 20px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.contentLeft::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(135deg, 
    rgba(0, 198, 255, 0.05) 0%, 
    transparent 30%, 
    transparent 70%, 
    rgba(0, 242, 241, 0.05) 100%);
  pointer-events: none;
}

.el-sub-menu /deep/ .el-menu {
  background-color: rgba(0, 21, 42, 1);
}

.contentLeft .el-sub-menu .el-menu li {
  width: 100%;
  box-sizing: inherit;
  background: rgba(0, 21, 42, 1);
}

.contentLeft /deep/ .el-menu-item,
.contentLeft /deep/ .el-sub-menu__title {
  height: 45px;
  line-height: 45px;
  color: white;
  border-radius: 25px;
  font-size: 14px;
  font-weight: 500;
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
  overflow: hidden;
  margin: 5px 10px;
  padding: 0 15px;
  letter-spacing: 0.5px;
}

.contentLeft /deep/ .el-menu-item::before,
.contentLeft /deep/ .el-sub-menu__title::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.1), transparent);
  transition: left 0.5s;
}

.contentLeft /deep/ .el-menu-item:hover::before,
.contentLeft /deep/ .el-sub-menu__title:hover::before {
  left: 100%;
}

.contentLeft /deep/ .el-sub-menu__title:hover {
  background: linear-gradient(135deg, rgba(53, 107, 226, 0.9), rgba(0, 242, 241, 0.9)) !important;
  border-radius: 25px;
  transform: translateX(8px) scale(1.02);
  box-shadow: 0 8px 25px rgba(53, 107, 226, 0.4);
  color: white !important;
}

.contentLeft /deep/ .el-menu-item:hover {
  background: linear-gradient(135deg, rgba(53, 107, 226, 0.9), rgba(0, 242, 241, 0.9)) !important;
  border-radius: 25px;
  transform: translateX(8px) scale(1.02);
  box-shadow: 0 8px 25px rgba(53, 107, 226, 0.4);
  color: white !important;
}

.contentLeft /deep/ .el-menu-item.is-active {
  background: linear-gradient(135deg, rgba(53, 107, 226, 1), rgba(0, 242, 241, 1)) !important;
  border-radius: 25px;
  box-shadow: 0 8px 30px rgba(53, 107, 226, 0.5);
  transform: translateX(5px) scale(1.05);
  color: white !important;
  font-weight: 600;
}

.contentLeft /deep/ .el-menu-item i,
.contentLeft /deep/ .el-sub-menu__title i {
  margin-right: 8px;
  font-size: 16px;
  transition: all 0.3s ease;
}

.contentLeft /deep/ .el-menu-item:hover i,
.contentLeft /deep/ .el-sub-menu__title:hover i {
  transform: scale(1.1);
  color: white;
}

.contentLeft /deep/ .el-menu-item.is-active i {
  transform: scale(1.1);
  color: white;
}

.contentLeft /deep/ .el-sub-menu .el-menu {
  background: rgba(0, 21, 42, 0.8) !important;
  border-radius: 15px;
  margin: 5px 0;
  padding: 5px 0;
}

.contentLeft /deep/ .el-sub-menu .el-menu-item {
  margin: 2px 15px;
  border-radius: 20px;
  font-size: 13px;
  height: 35px;
  line-height: 35px;
}

@keyframes breathe {
  0%, 100% { opacity: 0.8; }
  50% { opacity: 1; }
}

.contentLeft /deep/ .el-menu-item.is-active {
  animation: breathe 2s ease-in-out infinite;
}

.contentLeft /deep/ .el-menu-item,
.contentLeft /deep/ .el-sub-menu__title {
  animation: slideInLeft 0.5s ease-out;
}

@keyframes slideInLeft {
  from {
    opacity: 0;
    transform: translateX(-20px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

.yingyong-page {
  height: 100%;
  flex: 1;
  overflow: auto;
  background: linear-gradient(135deg, rgba(241, 249, 254, 0.8) 0%, rgba(195, 207, 226, 0.3) 100%);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  position: relative;
  z-index: 5;
}

.page-title-box {
  height: 60px;
  line-height: 60px;
  color: #00152A;
  font-size: 18px;
  font-weight: 600;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid rgba(255, 255, 255, 0.2);
  position: relative;
  z-index: 10;
  animation: slideInDown 0.6s cubic-bezier(0.4, 0, 0.2, 1);
}

@keyframes slideInDown {
  from {
    opacity: 0;
    transform: translateY(-20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.title-icon {
  height: 35px;
  margin: 12.5px 5px 0 10px;
}

.list-content {
  height: calc(100% - 70px);
  overflow: auto;
}

.sort-box {
  padding: 10px;
}

.sort-box /deep/ .el-radio-button__inner {
  padding: 0 24px;
  height: 36px;
  line-height: 36px;
}

.sort-box /deep/ .el-radio-button__original-radio:checked+.el-radio-button__inner {
  color: white;
  background: linear-gradient(135deg, #3B82F6, #1D4ED8);
  box-shadow: 0 2px 4px rgba(59, 130, 246, 0.3);
}

.list-box {
  padding-left: 10px;
  padding-top: 10px;
  overflow: hidden;
}

/* 苹果风格应用卡片设计 */
.app-card {
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.95), rgba(248, 250, 252, 0.9));
  border-radius: 20px;
  padding: 24px;
  margin-bottom: 20px;
  margin-right: 20px;
  width: calc(25% - 20px);
  height: 320px;
  float: left;
  box-shadow: 
    0 1px 3px rgba(0, 0, 0, 0.1),
    0 1px 2px rgba(0, 0, 0, 0.06);
  border: 1px solid rgba(0, 0, 0, 0.05);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
  overflow: hidden;
  animation: slideInUp 0.6s cubic-bezier(0.4, 0, 0.2, 1) both;
  display: flex;
  flex-direction: column;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}

.app-card:hover {
  transform: translateY(-4px);
  box-shadow: 
    0 10px 25px rgba(0, 0, 0, 0.1),
    0 4px 6px rgba(0, 0, 0, 0.05);
  border-color: rgba(59, 130, 246, 0.2);
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.98), rgba(248, 250, 252, 0.95));
}

.app-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.1), transparent);
  transition: left 0.6s;
}

.app-card:hover {
  transform: translateY(-8px) scale(1.02);
  box-shadow: 0 20px 40px rgba(0, 60, 126, 0.2);
}

.app-card:hover::before {
  left: 100%;
}

@keyframes slideInUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 卡片头部 - 更紧凑 */
.app-card-header {
  display: flex;
  align-items: center;
  margin-bottom: 12px;
  padding-bottom: 8px;
  border-bottom: 1px solid rgba(0, 0, 0, 0.05);
}

.app-icon {
  width: 40px;
  height: 40px;
  background: linear-gradient(135deg, #007AFF, #5AC8FA);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  color: white;
  margin-right: 12px;
  box-shadow: 
    0 2px 8px rgba(0, 122, 255, 0.2),
    0 1px 3px rgba(0, 0, 0, 0.1);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
  overflow: hidden;
}

.app-icon::before {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: linear-gradient(45deg, transparent, rgba(255, 255, 255, 0.1), transparent);
  transform: rotate(45deg);
  transition: all 0.6s ease;
  opacity: 0;
}

.app-card:hover .app-icon::before {
  opacity: 1;
  animation: shimmer 1.5s ease-in-out;
}

@keyframes shimmer {
  0% {
    transform: translateX(-100%) translateY(-100%) rotate(45deg);
  }
  100% {
    transform: translateX(100%) translateY(100%) rotate(45deg);
  }
}

.app-card:hover .app-icon {
  transform: scale(1.1) rotate(5deg);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.15);
  filter: brightness(1.1) saturate(1.2);
}

.app-title {
  font-size: 16px;
  font-weight: 700;
  color: #003C7E;
  line-height: 1.3;
  flex: 1;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* 精选应用标识 */
.featured-badge {
  color: #FFD700;
  font-size: 14px;
  margin-left: 8px;
  animation: sparkle 2s ease-in-out infinite;
}

@keyframes sparkle {
  0%, 100% { 
    transform: scale(1);
    opacity: 1;
  }
  50% { 
    transform: scale(1.2);
    opacity: 0.8;
  }
}

/* 卡片内容 - 固定高度布局 */
.app-card-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: 4px 0;
}

.app-description {
  color: #333;
  font-size: 13px;
  line-height: 1.5;
  margin-bottom: 10px;
  height: 5.2em;
  display: -webkit-box;
  -webkit-line-clamp: 4;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
  position: relative;
}

/* 信息区域 - 2x3布局 */
.app-info-section {
  margin-top: 12px;
}

.info-row {
  display: flex;
  margin-bottom: 6px;
  width: 100%;
  gap: 4px;
}

.info-item {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 4px 6px;
  background: rgba(247, 250, 252, 0.8);
  border-radius: 4px;
  border: 1px solid rgba(226, 232, 240, 0.8);
  font-size: 11px;
  flex: 1;
  min-height: 28px;
  cursor: help;
  transition: all 0.2s ease;
}

.info-item:hover {
  background: rgba(59, 130, 246, 0.1);
  border-color: rgba(59, 130, 246, 0.3);
  transform: translateY(-1px);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.info-icon {
  font-size: 12px;
  margin-right: 4px;
  opacity: 0.8;
}

.info-value {
  color: #2d3748;
  font-weight: 600;
  font-size: 10px;
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
}

/* 操作按钮区域 - 更紧凑 */
.app-card-actions {
  display: flex;
  gap: 6px;
  padding-top: 10px;
  border-top: 1px solid rgba(0, 0, 0, 0.05);
}

.action-btn {
  flex: 1;
  height: 32px;
  border-radius: 16px;
  font-size: 12px;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
  overflow: hidden;
  border: none;
  cursor: pointer;
}

.action-btn::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.3), transparent);
  transition: left 0.5s;
}

.action-btn:hover::before {
  left: 100%;
}

.secondary-btn {
  background: #f7fafc;
  color: #4a5568;
  border: 1px solid #e2e8f0;
}

.secondary-btn:hover {
  background: #edf2f7;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.primary-btn {
  background: linear-gradient(135deg, #10B981, #34D399);
  color: white;
  box-shadow: 
    0 2px 8px rgba(16, 185, 129, 0.2),
    0 1px 3px rgba(0, 0, 0, 0.1);
  border: none;
  font-weight: 600;
}

.primary-btn:hover {
  transform: translateY(-1px);
  box-shadow: 
    0 4px 12px rgba(16, 185, 129, 0.3),
    0 2px 4px rgba(0, 0, 0, 0.1);
  background: linear-gradient(135deg, #059669, #10B981);
}

.btn-icon {
  font-size: 12px;
}

.empty-state {
  text-align: center;
  padding: 60px 20px;
  background: white;
  border-radius: 10px;
  margin-bottom: 20px;
}

.empty-icon {
  font-size: 48px;
  margin-bottom: 16px;
}

.empty-title {
  font-size: 18px;
  color: #333;
  margin-bottom: 8px;
  font-weight: 600;
}

.empty-desc {
  font-size: 14px;
  color: #666;
  margin-bottom: 24px;
}
</style>
